#include "paintlistwidget.h"
#include "ui_paintlistwidget.h"
#include "cmenubutton.h"

#include <QBitmap>
#include <QScreen>

PaintListWidget::PaintListWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PaintListWidget)
{
    ui->setupUi(this);

    initUI();
}

void PaintListWidget::initUI()
{
    setAttribute(Qt::WA_DeleteOnClose);
    setWindowFlags(Qt::FramelessWindowHint | windowFlags() | Qt::Tool);
    setWindowFlags(this->windowFlags() | Qt::WindowStaysOnTopHint);

    QBitmap bmp(this->size());
    bmp.fill();
    QPainter p(&bmp);
    p.setPen(Qt::NoPen);
    p.setBrush(Qt::black);
    p.drawRoundedRect(bmp.rect(),5,5);
    setMask(bmp);

    //normal pen
    ui->button_pen->setNormalImg("new/icon/image/icon_brush_default.png");
    ui->button_pen->setPressImg("new/icon/image/icon_brush_click.png");
    ui->button_pen->setButtonTxt("Pen");
    ui->button_pen->needSelect = true;
    ui->button_pen->setButtonSelectStatus();

    //nite writer pen
    ui->button_niteWriterPen->setNormalImg("new/icon/image/icon_highlighter_default.png");
    ui->button_niteWriterPen->setPressImg("new/icon/image/icon_highlighter_click.png");
    ui->button_niteWriterPen->setButtonTxt("Nite Pen");
    ui->button_niteWriterPen->needSelect = true;
    //eraser
    ui->button_eraser->setNormalImg("new/icon/image/icon_eraser_default.png");
    ui->button_eraser->setPressImg("new/icon/image/icon_eraser_click.png");
    ui->button_eraser->setButtonTxt("Eraser");
    ui->button_eraser->needSelect = true;
    //revocation
    ui->button_revocation->setNormalImg("new/icon/image/icon_revoke_default.png");
    ui->button_revocation->setPressImg("new/icon/image/icon_revoke_click.png");
    ui->button_revocation->setButtonTxt("Revoke");
    //paintBackground
    ui->button_paintBackground->setNormalImg("new/icon/image/icon_bg_default.png");
    ui->button_paintBackground->setPressImg("new/icon/image/icon_bg_click.png");
    ui->button_paintBackground->setButtonTxt("Bg");
    //paintClear
    ui->button_paintClear->setNormalImg("new/icon/image/icon_clear_default.png");
    ui->button_paintClear->setPressImg("new/icon/image/icon_clear_click.png");
    ui->button_paintClear->setButtonTxt("Clear");
    //Exit
    ui->button_exit->setNormalImg("new/icon/image/icon_exit_default.png");
    ui->button_exit->setPressImg("new/icon/image/icon_exit_click.png");
    ui->button_exit->setButtonTxt("Exit");

    //SIGNAL
    connect(ui->button_pen, SIGNAL(btnClicked()), this, SLOT(onBtnPenClicked()));
    connect(ui->button_niteWriterPen, SIGNAL(btnClicked()), this, SLOT(onBtnNitePenClicked()));
    connect(ui->button_eraser, SIGNAL(btnClicked()), this, SLOT(onBtnEraserClicked()));
    connect(ui->button_revocation, SIGNAL(btnClicked()), this, SLOT(onBtnRevokeClicked()));
    connect(ui->button_paintBackground, SIGNAL(btnClicked()), this, SLOT(onBtnBgClicked()));
    connect(ui->button_paintClear, SIGNAL(btnClicked()), this, SLOT(onBtnClearClicked()));
    connect(ui->button_exit, SIGNAL(btnClicked()), this, SLOT(onBtnExitClicked()));
    connect(ui->button_pen_select, SIGNAL(btnClicked()), this, SLOT(onBtnSelectPenClicked()));
    connect(ui->button_nite_pen_select, SIGNAL(btnClicked()), this, SLOT(onBtnSelectNitePenClicked()));
}

void PaintListWidget::setPenSelected() {
    ui->button_pen->setButtonSelectStatus();
    ui->button_niteWriterPen->setButtonNolmalStatus();
    ui->button_eraser->setButtonNolmalStatus();
}

void PaintListWidget::setNitePenSelected() {
    ui->button_pen->setButtonNolmalStatus();
    ui->button_niteWriterPen->setButtonSelectStatus();
    ui->button_eraser->setButtonNolmalStatus();
}

void PaintListWidget::onBtnPenClicked() {
    ui->button_pen->setButtonSelectStatus();
    ui->button_niteWriterPen->setButtonNolmalStatus();
    ui->button_eraser->setButtonNolmalStatus();

    emit penClicked();
}

void PaintListWidget::onBtnNitePenClicked() {
    ui->button_pen->setButtonNolmalStatus();
    ui->button_niteWriterPen->setButtonSelectStatus();
    ui->button_eraser->setButtonNolmalStatus();

    emit nitePenClicked();
}

void PaintListWidget::onBtnEraserClicked() {
    ui->button_pen->setButtonNolmalStatus();
    ui->button_niteWriterPen->setButtonNolmalStatus();
    ui->button_eraser->setButtonSelectStatus();

    emit eraserClicked();
}

void PaintListWidget::onBtnRevokeClicked() {
    emit revokeClicked();
}

void PaintListWidget::onBtnBgClicked() {
    emit bgClicked();
}

void PaintListWidget::onBtnClearClicked() {
    emit clearClicked();
}

void PaintListWidget::onBtnExitClicked() {
    emit exitClicked();
}

void PaintListWidget::onBtnSelectPenClicked() {
    emit selectPenClicked();
}

void PaintListWidget::onBtnSelectNitePenClicked() {
    emit selectNitePenClicked();
}

void PaintListWidget::mousePressEvent(QMouseEvent *event)
{
     if (event->button() == Qt::LeftButton)
     {
         m_bPressed = true;
         m_point = event->pos();
     }
}

void PaintListWidget::mouseReleaseEvent(QMouseEvent *event)
{
    Q_UNUSED(event);
    m_bPressed = false;
}

void PaintListWidget::mouseMoveEvent(QMouseEvent *event)
{
    if (m_bPressed)
        move(event->pos() - m_point + pos());
}

PaintListWidget::~PaintListWidget()
{
    delete ui;
}
